<template>
  <div class="app-container">
    <!-- 头部搜索区域 -->
    <div class="search_and_new">
      <div class="put">
        <el-input v-model="searchKey" placeholder="请输入字典名称或类型" style="width:250px;" />
      </div>
      <div class="new_btn">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">添加字典</el-button>
      </div>
      <el-button type="primary" size="mini" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button type="primary" size="mini" @click="getDictList">刷新</el-button>
      <el-button type="primary" size="mini" @click="handleReset">重置</el-button>
    </div>

    <!-- 表格区域 -->
    <el-table v-loading="listLoading" :data="dictList" stripe style="width:100%">
      <el-table-column type="index" width="60" label="序号" />
      <el-table-column prop="title" label="字典名称" width="200" />
      <el-table-column prop="type" label="字典类型" width="200" />
      <el-table-column prop="description" label="备注" min-width="200" />
      <el-table-column prop="sortOrder" label="排序" width="100" />
      <el-table-column label="操作" width="300" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleViewData(scope.row)">数据管理</el-button>
          <el-button type="success" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑字典弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" :close-on-click-modal="false" width="30%">
      <el-form ref="dictForm" :model="dictForm" :rules="dictRules" label-width="100px">
        <el-form-item label="字典名称" prop="title">
          <el-input v-model="dictForm.title" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="type">
          <el-input v-model="dictForm.type" placeholder="请输入字典类型" :disabled="isEdit" />
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="dictForm.description" type="textarea" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="排序" prop="sortOrder">
          <el-input-number v-model="dictForm.sortOrder" :min="0" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAllDict, addDict, editDict, deleteDict, searchDict } from '@/api/dict'

export default {
  name: 'DictManage',
  data() {
    return {
      searchKey: '',
      listLoading: false,
      dictList: [],
      dialogVisible: false,
      dialogTitle: '添加字典',
      isEdit: false,
      dictForm: {
        id: '',
        title: '',
        type: '',
        description: '',
        sortOrder: 0
      },
      dictRules: {
        title: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请输入字典类型', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getDictList()
  },
  methods: {
    // 获取字典列表
    getDictList() {
      this.listLoading = true
      getAllDict().then(res => {
        if (res.code === 200) {
          this.dictList = res.result
        }
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 搜索
    handleSearch() {
      if (!this.searchKey) {
        this.$message.warning('请输入搜索关键词')
        return
      }
      this.listLoading = true
      searchDict(this.searchKey).then(res => {
        if (res.code === 200) {
          this.dictList = res.result
        }
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 重置
    handleReset() {
      this.searchKey = ''
      this.getDictList()
    },
    // 添加字典
    handleAdd() {
      this.dialogTitle = '添加字典'
      this.isEdit = false
      this.dictForm = {
        title: '',
        type: '',
        description: '',
        sortOrder: 0
      }
      this.dialogVisible = true
    },
    // 编辑字典
    handleEdit(row) {
      this.dialogTitle = '编辑字典'
      this.isEdit = true
      this.dictForm = {
        id: row.id,
        title: row.title,
        type: row.type,
        description: row.description,
        sortOrder: row.sortOrder
      }
      this.dialogVisible = true
    },
    // 提交
    handleSubmit() {
      this.$refs.dictForm.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            editDict(this.dictForm).then(res => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.dialogVisible = false
                this.getDictList()
              }
            })
          } else {
            addDict(this.dictForm).then(res => {
              if (res.code === 200) {
                this.$message.success('添加成功')
                this.dialogVisible = false
                this.getDictList()
              }
            })
          }
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确定要删除该字典吗？删除后对应的字典数据也会被删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDict(row.id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.getDictList()
          }
        })
      })
    },
    // 查看字典数据
    handleViewData(row) {
      this.$router.push({
        path: '/dict/data',
        query: { dictId: row.id, dictType: row.type, dictTitle: row.title }
      })
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.search_and_new {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.put {
  flex: 1;
}
</style>
